package at.codebase.web.util.json;

import java.util.ArrayList;
import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import at.codebase.core.search.SearchItem;
import at.codebase.db.entities.EItem;
import at.codebase.db.entities.EUser;

import com.google.appengine.api.search.GeoPoint;

/**
 * @author Daniel Marth <danielmarth@gmx.at>
 */
public class JsonDecoder {

	public static EUser decodeUser(String userDataString) {
		JSONObject jObj = (JSONObject) JSONValue.parse(userDataString);
		
		String googleUserId = (String) jObj.get(JsonKeys.GOOGLE_USER_ID);
		String username = (String) jObj.get(JsonKeys.USERNAME);
		String email = (String) jObj.get(JsonKeys.MAIL);
		String description = (String) jObj.get(JsonKeys.DESCRIPTION);
		
		return EUser.createImportUser(googleUserId, username, email, description);
	}

	public static List<EItem> decodeUserItems(String userDataString) {
		JSONArray jArray = (JSONArray) ((JSONObject) JSONValue
				.parse(userDataString)).get(JsonKeys.ITEMS);
		List<EItem> itemList = new ArrayList<EItem>();
		if(jArray != null){
			for (Object obj : jArray) {
				JSONObject jObj = (JSONObject) obj;
				itemList.add(decodeItem(jObj));
			}
		}
		return itemList;
	}

	public static EItem decodeItem(JSONObject jObj) {		
		String itemTitle = (String) jObj.get(JsonKeys.TITLE);
		String itemDescription = (String) jObj.get(JsonKeys.ITEM_DESCRIPTION);
		
		double itemPrice = 0.0;
		boolean invalidPrice = false;
		try{
			itemPrice = ((Double) jObj.get(JsonKeys.PRICE)).doubleValue();
		}catch(ClassCastException e){
			invalidPrice = true;
		}

		EItem item = EItem.createImportItem(null, itemTitle, itemDescription, itemPrice);
		if(invalidPrice)
			item.setActive(false);
		return item;
	}

	public static List<EItem> decodeItems(String string, int limit) {
		List<EItem> items = new ArrayList<EItem>();
		JSONObject obj = (JSONObject) JSONValue.parse(string);
		if (obj == null) {
			return items;
		}
		JSONArray sponsoredItems = (JSONArray) obj.get("sponsoredItems");
		JSONArray regularItems = (JSONArray) obj.get("items");
		int i = 0;
		for (; sponsoredItems != null && i < limit && i < sponsoredItems.size(); i++) {
			items.add(decodeItem((JSONObject) sponsoredItems.get(i)));
		}
		for (int j = 0; regularItems != null && i < limit
				&& j < regularItems.size(); i++, j++) {
			items.add(decodeItem((JSONObject) regularItems.get(j)));
		}
		return items;
	}

	public static List<SearchItem> decodeSearchItems(String string, int limit) {
		List<SearchItem> items = new ArrayList<SearchItem>();
		JSONObject obj = (JSONObject) JSONValue.parse(string);
		if (obj == null) {
			return items;
		}
		JSONArray sponsoredItems = (JSONArray) obj.get("sponsoredItems");
		JSONArray regularItems = (JSONArray) obj.get("items");
		int i = 0;
		for (; sponsoredItems != null && i < limit && i < sponsoredItems.size(); i++) {
			items.add(decodeSearchItem((JSONObject) sponsoredItems.get(i)));
		}
		for (int j = 0; regularItems != null && i < limit
				&& j < regularItems.size(); i++, j++) {
			items.add(decodeSearchItem((JSONObject) regularItems.get(j)));
		}
		return items;
	}

	public static SearchItem decodeSearchItem(JSONObject jsonObject) {
		SearchItem item = new SearchItem();
		item.setItemId(jsonObject.get(JsonKeys.ID).toString());
		item.setTitle((String) jsonObject.get(JsonKeys.TITLE));
		item.setPrice((String) jsonObject.get(JsonKeys.PRICE));
		item.setUrl((String) jsonObject.get(JsonKeys.URL));
		item.setDescription((String)jsonObject.get(JsonKeys.DESCRIPTION));
		String image = (String) jsonObject.get(JsonKeys.IMAGE);
		if (image != null && !image.isEmpty()) {
			item.setImage((String) jsonObject.get(JsonKeys.IMAGE));
		}
		Object location = jsonObject.get(JsonKeys.LOCATION);
		if (location != null) {
			item.setLocation((GeoPoint) location);
		}
		return item;
	}

}
